<?

// Add the Meta Box
function add_property_image() {
    add_meta_box(
        'property_image',
        'Upload image Banner',
        'show_property_image',
        'property',
        'normal',
        'high');
}
add_action('add_meta_boxes', 'add_property_image');

$prefix = 'zon_';
$custom_meta_fields = array(
    array(
        'label' => 'Upload images banner',
        'desc'  => 'Please select image banner or use image default',
        'id'    => $prefix.'image',
        'type'  => 'image'
    )
);

// enqueue scripts and styles, but only if is_admin
if(is_admin()) {
    wp_enqueue_script('jquery-ui-slider');
    //wp_enqueue_script('custom-js123', INCLUDES_DIR.'/posttypes/property/js/custom-js.js');
}

// The Callback
function show_property_image() {
    global $custom_meta_fields, $post;
    // Use nonce for verification
    echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
    
    // Begin the field table and loop
    echo '<table class="form-table">';
    foreach ($custom_meta_fields as $field) {
        // get value of this field if it exists for this post
        $meta = get_post_meta($post->ID, $field['id'], true);
        // begin a table row with
        echo '<tr>
                <td>';
                switch($field['type']) {
                    // image
                    case 'image':
                        $image = get_template_directory_uri().'/images/image.png';  
                        echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
                        if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }               
                        echo    '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" />
                                        <input class="custom_upload_image_button button" type="button" value="Choose Image" />
                                        <small>&nbsp;<a href="#" class="custom_clear_image_button">Remove Image</a></small>
                                        <br clear="all" /><span class="description">'.$field['desc'].'</span></br></br>
                                        <img src="'.$image.'" class="custom_preview_image" alt="" /><br />';
                    break;
                } //end switch
        echo '</td></tr>';
    } // end foreach
    echo '</table>'; // end table
}
// Save the Data
function save_property_image($post_id) {
    global $custom_meta_fields;
    
    // verify nonce
    if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__))) 
        return $post_id;
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return $post_id;
    // check permissions
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id))
            return $post_id;
        } elseif (!current_user_can('edit_post', $post_id)) {
            return $post_id;
    }
    
    // loop through fields and save the data
    foreach ($custom_meta_fields as $field) {
        if($field['type'] == 'tax_select') continue;
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];
        if ($new && $new != $old) {
            update_post_meta($post_id, $field['id'], $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);
        }
    } // enf foreach
}
add_action('save_post', 'save_property_image');

?>